package org.zachary.prosocialbehaviorservice.application;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zachary.prosocialbehaviorservice.application.command.ActivityItemAddCommand;
import org.zachary.prosocialbehaviorservice.application.command.ModifyActivityItemCommand;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.ActivityItemWithCategoryDTO;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.Activity;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.ActivityItem;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.ActivityToItem;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.event.ActivityItemDeleteEvent;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.event.ActivityLinkItemDelete;
import org.zachary.prosocialbehaviorservice.domain.repository.ActivityItemRepository;
import org.zachary.prosocialbehaviorservice.domain.repository.ActivityRepository;
import org.zachary.prosocialbehaviorservice.domain.repository.ActivityToItemRepository;
import org.zachary.prosocialbehaviorservice.infrastructure.utils.HiloGenerator;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Slf4j
@Service
public class ActivityItemService {
    private final ApplicationEventPublisher applicationEventPublisher;
    private final HiloGenerator hiloGenerator;
    private final ActivityItemRepository activityItemRepository;
    private final ActivityToItemRepository activityToItemRepository;
    private final ActivityRepository activityRepository;

    public ActivityItemService(ApplicationEventPublisher applicationEventPublisher, HiloGenerator hiloGenerator, ActivityItemRepository activityItemRepository, ActivityToItemRepository activityToItemRepository, ActivityRepository activityRepository){
        this.applicationEventPublisher = applicationEventPublisher;

        this.hiloGenerator = hiloGenerator;
        this.activityItemRepository = activityItemRepository;
        this.activityToItemRepository = activityToItemRepository;
        this.activityRepository = activityRepository;
    }
    @Transactional
    public void addActivityItem(Integer activityId, ActivityItemAddCommand command) {
        Optional<Activity> activity = activityRepository.findById(activityId);
        if (activity.isEmpty()) {
            throw new RuntimeException("Activity not found");
        }

        Integer itemId=hiloGenerator.generateId();

        ActivityItem item=ActivityItem.buildForAdd(itemId,command.title(),command.content(),command.startDate(),command.endDate(),command.qrCode(),command.number());
        item.addActivityItem();
        activityItemRepository.save(item);

        ActivityToItem to=ActivityToItem.of(activityId,itemId);

        to.add();
        activityToItemRepository.save(to);
    }
    @Transactional
    public void delete(Integer itemId) {
        activityItemRepository.deleteById(itemId);
        applicationEventPublisher.publishEvent(new ActivityItemDeleteEvent(itemId));
        activityToItemRepository.deleteById(itemId);
        applicationEventPublisher.publishEvent(new ActivityLinkItemDelete(itemId));
    }
    @Transactional
    public void modify(Integer activityId, ModifyActivityItemCommand command) {
        ActivityToItem oldObj=activityToItemRepository.findByItemId(command.itemId()).orElse(null);
        if(oldObj==null){
            log.error("activityToItemRepository.findByItemId(command.itemId()).orElse(null);的返回值为null");
            throw new RuntimeException("出现异常，请联系管理员");
        }
        if(!oldObj.getActivityId().equals(activityId))
        {
            activityToItemRepository.save(new ActivityToItem(oldObj.getId(),activityId,command.itemId()));
        }
        ActivityItem item=ActivityItem.buildForAdd(command.itemId(),command.title(),command.content(),command.startDate(),command.endDate(),command.qrCode(),command.number());
        item.modifyActivityItem();
        activityItemRepository.save(item);
    }
    @Transactional
    public ActivityItem findById(Integer itemId) {
        ActivityItem item=activityItemRepository.findById(itemId);
        if(item==null){
            log.error("查询结果为空");
        }
        return item;
    }
    @Transactional
    public Optional<Integer> findActivityIdByItemId(Integer id) {
        return activityToItemRepository.findActivityIdByItemId(id);
    }

}
